package com.yuexunit.finance.system.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.system.entity.FinanceLetterEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
*@version 创建时间：2017年2月8日 下午6:41:25
*@author wenwenlong
*类说明
*/
@Implements(FinanceLetterService.class)
public class FinanceLetterServiceImpl implements FinanceLetterService{

	@Override
	@Transactional
	public void createFinanceLetter(FinanceLetterEntity entity) {
		entity.setLetterType(5);
		entity.setReadFlag(Boolean.FALSE);
		entity.setCreateDate(DateUtil.now());
		entity.setCreateDateInt(DateUtil.todayInt());
		entity.setUpdateDate(DateUtil.now());
		PersistUtil.insert(entity);
	}

	@Override
	public FinanceLetterEntity inquireFinanceLetterDetail(Long letterId) {
		return PersistUtil.get(FinanceLetterEntity.class, letterId);
	}

	@Override
	public PageList<FinanceLetterEntity> inquireFinanceLetterPageList(Long receiverAccountId, Integer letterType, Boolean readFlag,
			PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceLetterEntity.class);
		if (letterType != null) {
			criteria.add(Restrictions.eq("letterType", letterType));
		}
		
		if (readFlag != null) {
			criteria.add(Restrictions.eq("readFlag", readFlag));
		}
		criteria.add(Restrictions.eq("receiverAccountId", receiverAccountId));
		criteria.addOrder(Order.desc("createDate"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<FinanceLetterEntity> inquireFinanceLetterList(Long receiverAccountId, Integer letterType, Boolean readFlag) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceLetterEntity.class);
		if (letterType != null) {
			criteria.add(Restrictions.eq("letterType", letterType));
		}
		
		if (readFlag != null) {
			criteria.add(Restrictions.eq("readFlag", readFlag));
		}
		criteria.add(Restrictions.eq("receiverAccountId", receiverAccountId));
		criteria.addOrder(Order.desc("createDate"));
		return PersistUtil.get(criteria);
	}
}
